<template>
  <div class="app-container">
    <div class="query-panel">
      <div class="query-panel-filter">
        <div class="query-form">
          <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入企业名称" clearable size="small"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-form>
        </div>
        <div class="query-btn">
          <el-row>
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-query-add" class="c-item-btn" size="mini" @click="handleAdd"
                v-hasPermi="['system:company:add']">新增</el-button>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="query-search">
        <div class="query-search-btn">
          <div class="query-search-btn">
            <el-button class="c-btn-reset" icon="el-icon-query-reset" size="mini" @click="resetQuery">重置</el-button>
            <el-button class="c-btn-search" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </div>
        </div>
      </div>
    </div>

    <el-table v-loading="loading" :data="List" stripe>
      <el-table-column label="企业名称" align="center" key="name" prop="name" min-width="160px"
        :show-overflow-tooltip="true" />
      <el-table-column label="企业简称" align="center" key="shortname" prop="shortname" :show-overflow-tooltip="true" />
      <el-table-column label="企业编码" align="center" key="code" prop="code" :show-overflow-tooltip="true" />
      <el-table-column label="企业法人" align="center" key="linkMan" prop="linkMan" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="320" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" class="r-item-btn" icon="el-icon-c-view" @click="handleView(scope.row)"
            v-hasPermi="['system:company:view']">查看</el-button>
          <el-button size="mini" class="r-item-btn" icon="el-icon-c-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:company:edit']">修改</el-button>
          <el-button size="mini" class="r-item-btn" icon="el-icon-c-delete" @click="handleDelete(scope.row)"
            v-if="scope.row.code != 'Sys'" v-hasPermi="['system:company:remove']">删除</el-button>
          <router-link icon="el-icon-location" target="_blank" :to="{
            path: '/map/companydraw',
            query: {
              companyid: scope.row.id,
            },
          }" class="link-type">
            <el-button size="mini" class="r-item-btn" icon="el-icon-location">地图标记
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="670px" append-to-body :close-on-click-modal="false">
      <el-form v-loading="oploading" ref="form" :model="form" :rules="rules" label-width="110px" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入企业名称" size="small" class="w-full" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业简称" prop="shortname">
              <el-input v-model="form.shortname" placeholder="请输入企业简称" size="small" class="w-full" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入企业编码" size="small" class="w-full" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业法人" prop="linkMan">
              <el-input v-model="form.linkMan" placeholder="请输入企业法人" size="small" class="w-full" />
            </el-form-item>
          </el-col>
        </el-row>
        <div v-if="opertype == 'add'">
          <el-row>
            <el-col :span="12">
              <el-form-item label="管理员登录名" prop="userName">
                <el-input v-model="form.userName" size="small" placeholder="请输入管理员登录名" class="w-full" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" size="small" placeholder="请输入密码" class="w-full" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="userFullName">
                <el-input v-model="form.userFullName" size="small" placeholder="请输入姓名" class="w-full" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="角色">
                <el-select v-model="form.roleids" placeholder="角色" clearable size="small" :multiple="true"
                  class="w-full" disabled="true">
                  <el-option v-for="item in rolesoptions" :key="item.id" :label="item.roleName" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancel-btn" @click="cancel">取 消</el-button>
        <el-button class="save-btn" @click="submitForm" v-if="!disabled">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listcompany,
  getcompany,
  addcompany,
  updatecompany,
  delcompany,
} from "@/api/system/company";
import { getlistUser } from "@/api/system/user";
import { getlistRole } from "@/api/system/role";
export default {
  name: "company",
  data() {
    return {
      // 遮罩层
      loading: true,
      oploading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //表格数据
      List: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      deptExpand: true,
      deptNodeAll: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        orderByColumn: "id",
        isAsc: "asc",
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "企业名称不能为空", trigger: "blur" },
        ],
        shortname: [
          { required: true, message: "企业简称不能为空", trigger: "blur" },
        ],
        code: [
          { required: true, message: "企业编码不能为空", trigger: "blur" },
        ],
        userName: [
          { required: true, message: "管理员登录名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
        userFullName: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
      },
      disabled: false,
      rolesoptions: [],
      opertype: undefined,
    };
  },
  async created() {
    var that = this;
    getlistRole({}).then((response) => {
      that.rolesoptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listcompany(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.List = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        code: undefined,
        shortname: undefined,
        linkMan: undefined,
        userName: undefined,
        password: undefined,
        userFullName: undefined,
        roleids: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.roleids = [2069];
      this.disabled = false;
      this.open = true;
      this.title = "添加企业";
      this.opertype = "add";

      this.$nextTick(() => {
        this.dialogIcon("add");
      });
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      await getcompany(id).then((response) => {
        this.form = response.data;
        this.disabled = false;
        this.open = true;
        this.title = "修改企业";

        this.$nextTick(() => {
          this.dialogIcon("edit");
        });
      });

      await getlistUser({ companyid: id, issys: 1 }).then((response) => {
        if (response.data != null && response.data.length > 0) {
          this.form.userName = response.data[0].userName;
          this.form.userFullName = response.data[0].userFullName;
        }
      });
      this.opertype = "update";
    },
    /** 查看按钮操作 */
    async handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      await getcompany(id).then((response) => {
        this.form = response.data;
        this.disabled = true;
        this.open = true;
        this.title = "查看企业";

        this.$nextTick(() => {
          this.dialogIcon("view");
        });
      });

      await getlistUser({ companyid: id, issys: 1 }).then((response) => {
        if (response.data != null && response.data.length > 0) {
          this.form.userName = response.data[0].userName;
          this.form.userFullName = response.data[0].userFullName;
        }
      });
      this.opertype = "update";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updatecompany(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addcompany(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delcompany(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
<style scoped>
.el-select {
  width: 100%;
}
</style>